//
//  ContentPictureView.m
//  Marketing
//
//  Created by Hanen 3G 01 on 16/5/23.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "ContentPictureView.h"
#define imageH [UIView getWidth:40] // 图片高度
#define imageW [UIView getWidth:40] // 图片宽度
@interface ContentPictureView ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    UIScrollView  *imagesScrolleview;
    // 标识被编辑的按钮 -1 为添加新的按钮
    NSInteger editTag;
    
   
    CGFloat  space;
    
    //    NSMutableArray  *_allPicArray;
}
@end

@implementation ContentPictureView


- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _allPicArray = [NSMutableArray array];
        self.layer.borderWidth = 0;
        self.layer.borderColor = [UIColor whiteColor].CGColor;
//        self.contentMode = UIViewContentModeScaleToFill;
        imagesScrolleview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width , self.height)];
//        imagesScrolleview.backgroundColor = NavBackColor;
//        imagesScrolleview
        imagesScrolleview.showsHorizontalScrollIndicator = NO;
//        imagesScrolleview.delaysContentTouches = YES;
        [self addSubview:imagesScrolleview];
        
        [self addSubviews];
        if (KSCreenW == 320 ) {
            space = [UIView getHeight:5.0f];
        }else{
            space = [UIView getHeight:10.0f];
        }
        
        for (int i = 0; i< imagesScrolleview.subviews.count; i++) {
            id  vi = imagesScrolleview.subviews[i];
            NSLog(@"%@",[vi class]);
        }
    }
    return self;
}

//- (void)setIsHadCheck:(BOOL)isHadCheck
//{
//    _isHadCheck = isHadCheck;
//    if (!_isHadCheck) {
//        [self addSubviews];
//    }
//}
- (void)addSubviews
{
    
    _addBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, [UIView getWidth:10.0f],imageW , imageW)];
    
    _addBtn.tag = 15151515;
    //    [_addBtn setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
    [_addBtn setImage:[UIImage imageNamed:@"HY_list_btn_add_pic_normal"] forState:UIControlStateNormal];
    [_addBtn setImage:[UIImage imageNamed:@"HY_list_btn_add_pic_pressed"] forState:UIControlStateHighlighted];
    [_addBtn addTarget:self action:@selector(addPic) forControlEvents:UIControlEventTouchUpInside];
    
    [imagesScrolleview addSubview:_addBtn];
    
}
- (void)drawRect:(CGRect)rect
{
    
    for (UIView *vi in imagesScrolleview.subviews) {
        if ([vi isKindOfClass:[UIButton class]]) {
            //
            if(vi.tag != 15151515){
                [vi removeFromSuperview];
            }
            
        }
    }
    //    NSLog(@"重绘之前 删除scrllview的图片 子视图剩余%ld",imagesScrolleview.subviews.count);
    //将按到的图片 放到总图片数组里面
    
    //#warning 子视图会重叠
    //    NSLog(@"需要排布的图片个数===%ld",_allPicArray.count);
    //数组是AlAsset 资源
    //获得等比缩略图 原图的等比缩略图[asset aspectRatioThumbnail];
    //       缩略图是相册中每张照片的poster图  [asset thumbnail];
    
    for (int i = 0; i< _allPicArray.count; i++) {
        //        UIImage *chooseImage = [images[i] aspectRatioThumbnail];
        NSData *data =_allPicArray[i];
        UIImage *img = [UIImage imageWithData:data];
        
        UIButton *imageView = [[UIButton alloc] initWithFrame:CGRectMake((imageW + space) * i, _addBtn.y, imageW, imageW)];
        imageView.tag = 1 + i;
        [imageView bringSubviewToFront:imagesScrolleview];
        [imageView setBackgroundImage:img forState:UIControlStateNormal];
        //        imageView.backgroundColor = BlueColor;
        imageView.hidden = NO;
        [imageView addTarget:self action:@selector(clickTodelete:) forControlEvents:UIControlEventTouchUpInside];
        [imagesScrolleview addSubview:imageView];
        if (i == _allPicArray.count - 1) {
            //            CGRect frame = _addBtn.frame;
            //            _addBtn.frame = CGRectMake(imageView.maxX + 10, imageView.y, frame.size.width, frame.size.height);
            
            //            CGAffineTransform transfom = CGAffineTransformMakeTranslation(imageView.maxX + 10, imageView.y);
            //            _addBtn.transform = transfom;
            //            if (_addBtn.maxX > kScreenWidth) {
            //                CGRect rect = self.frame;
            //                rect.size.height +=
            //            }
            imagesScrolleview.contentSize = CGSizeMake(imageView.maxX + 10 + imageW, imagesScrolleview.height);
        }
        
    }
    [self refreshAddBtnFrame];
    NSLog(@"view的子视图的个数%ld",imagesScrolleview.subviews.count);
    //    for (int i = 0; i< _allPicArray.count   ; i++) {
    ////        NSLog(@"%@",_allPicArray[i]);
    //    }
}

- (void)refreshAddBtnFrame
{
    CGRect frame = _addBtn.frame;
    frame.origin.x = (imageW + space) * self.allPicArray.count;
    [UIView animateWithDuration:0.1 animations:^{
        _addBtn.frame = frame;
    }];
}
- (void)addPic
{
    if ([self.delegate respondsToSelector:@selector(addImage2)]) {
        [self.delegate addImage2];
    }
}
- (void)setImages:(NSMutableArray *)images
{
    _images = images;
    //    _addBtn.hidden = YES;
    
//    NSLog(@"新照片加入之前 数组 中的个数 %ld",_allPicArray.count);
    for (int i = 0; i< self.images.count; i++) {
        
        //        UIImage *ima = [UIImage imageWithCGImage:[self.images[i] fullResolutionImage]];
        //        NSData *data = UIImageJPEGRepresentation(ima, 0.5);
        
        
        //        if (_allPicArray.count < 6) {
        [_allPicArray addObject:self.images[i]];
        //        }
        
    }
    //    for (int i = 0; i< _allPicArray.count; i++) {
    //        NSLog(@"所有图片的顺序 …%@",_allPicArray[i]);
    //    }
    [self setNeedsDisplay];
    //    NSLog(@"view的子视图的个数%ld",imagesScrolleview.subviews.count);
}
- (void)clickTodelete:(UIButton *)btn
{
    [btn removeFromSuperview];
    //    NSLog(@"tag == %d",btn.tag);
    [_allPicArray removeObjectAtIndex:btn.tag - 1];
    
    if (self.addBtn.hidden == YES) {
        self.addBtn.hidden = NO;
    }
    [self setNeedsDisplay];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
